// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CoursePlayAction.java

package com.iphaeton.lms.action.course.scorm12;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.common.helper.UserSession;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.course.scorm12.ICourseService;
import com.iphaeton.lms.business.course.scorm12.IItemService;
import com.iphaeton.lms.pojo.course.*;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.dom4j.io.XMLWriter;

// Referenced classes of package com.iphaeton.lms.action.course.scorm12:
//            CourseBizSupportAction

public class CoursePlayAction extends CourseBizSupportAction
{

    public CoursePlayAction()
    {
    }

    public ActionForward getCourseTree(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String root = request.getContextPath();
        String sn = request.getParameter("sn");
        org.dom4j.Document document = null;
        String id = request.getParameter("id");
        try
        {
            String courseId = course.findUserCourseById(id).getCourseId();
            document = item.getCourseTree(root, id, courseId, sn);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        response.setContentType("text/xml; charset=UTF-8");
        javax.servlet.ServletOutputStream sos = response.getOutputStream();
        XMLWriter writer = new XMLWriter(sos);
        writer.write(document);
        writer.flush();
        writer.close();
        return null;
    }

    public ActionForward courseStudyInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        String row = request.getParameter("row");
        Item last_item = null;
        java.util.List list = null;
        try
        {
            UserCourse uc = course.findUserCourseById(id);
            String item_id = uc.getItemId();
            if(item_id != null)
                last_item = item.findItemById(item_id);
            list = course.findCourseLabel(id, row);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("id", id);
        request.setAttribute("item", last_item);
        request.setAttribute("list", list);
        return mapping.findForward("course_start");
    }

    public ActionForward selectLabel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        String sequence = request.getParameter("sequence");
        String row = request.getParameter("row");
        java.util.List list;
        Item it;
        try
        {
            list = course.findCourseLabel(id, row);
            it = item.findItemBySequence(course.findUserCourseById(id).getCourseId(), Integer.parseInt(sequence));
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("id", id);
        request.setAttribute("list", list);
        request.setAttribute("item", it.getId());
        return mapping.findForward("label_list");
    }

    public ActionForward labelForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String item_id = request.getParameter("item_id");
        String id = request.getParameter("id");
        Item it;
        try
        {
            it = item.findItemById(item_id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("item", it);
        request.setAttribute("id", id);
        return mapping.findForward("create_label");
    }

    public ActionForward createLabel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        Calendar cal = Calendar.getInstance();
        java.util.Date time = cal.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = df.format(time);
        String id = request.getParameter("id");
        String row = request.getParameter("row");
        String name = request.getParameter("name");
        String item_id = request.getParameter("item");
        Label label = new Label();
        label.setName(name);
        label.setStuCourseId(id);
        label.setCreateTime(currentTime);
        label.setItemId(item_id);
        java.util.List list;
        try
        {
            label.setItem(item.findItemById(item_id));
            course.addLabel(label);
            list = course.findCourseLabel(id, row);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("id", id);
        request.setAttribute("item", item_id);
        request.setAttribute("list", list);
        return mapping.findForward("label_list");
    }

    public ActionForward removeLabel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        String row = request.getParameter("row");
        java.util.List list;
        Item it;
        Label label;
        try
        {
            label = course.findLabelById(id);
            course.removeLabel(id);
            list = course.findCourseLabel(label.getStuCourseId(), row);
            it = item.findItemById(label.getItemId());
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("id", label.getStuCourseId());
        request.setAttribute("item", it.getId());
        request.setAttribute("list", list);
        return mapping.findForward("label_list");
    }

    public ActionForward coursePlay(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String currentTime;
        String id;
        int se;
        Calendar cal = Calendar.getInstance();
        java.util.Date time = cal.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        currentTime = df.format(time);
        id = request.getParameter("id");
        se = Integer.parseInt(request.getParameter("sequence"));
        String courseId;
        int sequence;
        try {
        	 UserCourse uc = course.findUserCourseById(id);
             if(uc.getFirstTime() == null)
                 uc.setFirstTime(currentTime);
             uc.setLastTime(currentTime);
             course.persistent(uc);
             courseId = uc.getCourseId();
             sequence = course.queryCourseSequence(courseId);
             if(se > sequence)
                 return mapping.findForward("course_complete");

             for(int i = se; i <= sequence; i++)
             {
                 if(i < 0)
                     i = 0;
                 Item it = item.findItemBySequence(courseId, i);
                 if(item.isSco(it))
                     continue;
                 se = i;
                 request.setAttribute("url", courseId + File.separator + it.getLaunch());
                 break;
             }
             
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("id", id);
        request.setAttribute("sequence", new Integer(se));
        return mapping.findForward("course_play");
    }

    public ActionForward courseNote(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        java.util.List list = null;
        String courseId = null;
        try
        {
            String userId = ((UserSession)request.getSession().getAttribute("userSession")).getId();
            UserCourse uc = course.findUserCourseById(id);
            courseId = uc.getCourseId();
            list = course.findCourseNote(courseId, userId);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("courseId", courseId);
        request.setAttribute("list", list);
        return mapping.findForward("course_note");
    }

    public ActionForward removeNote(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        String note_id = request.getParameter("note_id");
        java.util.List list = null;
        try
        {
            String userId = ((UserSession)request.getSession().getAttribute("userSession")).getId();
            course.removeNote(note_id);
            list = course.findCourseNote(id, userId);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("courseId", id);
        request.setAttribute("list", list);
        return mapping.findForward("course_note");
    }

    public ActionForward editNote(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        com.iphaeton.lms.pojo.note.Note note = null;
        try
        {
            note = course.findNoteById(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("note", note);
        return mapping.findForward("course_note_edit");
    }

    public ActionForward getParam(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        String sequence = request.getParameter("sequence");
        Item it;
        try
        {
            it = item.findItemBySequence(course.findUserCourseById(id).getCourseId(), Integer.parseInt(sequence));
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("item", it);
        return mapping.findForward("item_param");
    }

    public void timeCounter(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        int sequence = Integer.parseInt(request.getParameter("sequence"));
        float period = Float.parseFloat(request.getParameter("period"));
        try
        {
            UserCourse uc = course.findUserCourseById(id);
            item.registerPeriod(uc, sequence, period);
        }
        catch(BusinessException e)
        {
            e.getMessage();
        }
    }

    private static Logger logger;
    private DynaActionForm daf;
    private Pagination pageInfo;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.action.course.scorm12.CoursePlayAction.class.getName());
    }
}
